<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" width="50%"
               :close-on-click-modal="false" :close-on-press-escape	='false'
               :title="form.id?'编辑服务器':'新增发送服务器'" center>
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="130px">
        <el-form-item label="名称" prop="showName">
          <el-input v-model="form.showName" size="small" placeholder="请输入服务器名称"></el-input>
        </el-form-item>
        <el-form-item label="SMTP服务器" prop="server">
          <el-input v-model="form.server" placeholder="请输入SMTP服务器"></el-input>
        </el-form-item>
        <el-form-item label="发件人姓名" prop="sendName">
          <el-input v-model="form.sendName" placeholder="请输入发件人姓名"></el-input>
        </el-form-item>
        <el-form-item label="email地址" prop="emailAddr">
          <el-input v-model="form.emailAddr" placeholder="请输入email地址"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮-->
      <div slot="footer" class="dialog-footer" align="right">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" size="mini"  @click="save" >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {add, update} from "@/api/mail/admin/mail-set";
    export default {
        name: "MailSetPut",
        data(){
            return{
                dialogVisible:false,
                form:{
                    showName:'',//显示名称,
                    server:'', //邮件服务器
                    sendName:'',//发送人姓名
                    emailAddr:'',//邮箱地址
                    // allowCustTypes:'', //允许的客户类型集合
                    // authorizationCode:'', //邮箱授权码
                    // dafaultAutograph:'', //默认签名
                    // imap4Port:'', //Imap4端口
                    // imap4Server:'', //Imap4服务器
                    // isAutoAddTrace:'',//发送后是否自动归并
                    // isAutoMerge:'',
                    // isCheckCustomType:'', //是否开启检查客户类型
                    // isEnable:'', //是否启用
                    // isGroupSend:'', //是否用于群发
                    // isSSL:'', //SSL加密
                    // isSpaLogin:'', //未知
                    // isStartTls:'', //是否使用StartTls传输
                    // isSyncImap4:'', //是否同步到Imap4服务器
                    // isUseProxy:'', //是否使用代理服务器
                    // isVerification:'',//是否验证
                    // passWord:'', //密码
                    // proxyPassWord:'', //代理服务器密码
                    // proxyPort:'', //代理服务器端口
                    // proxyServer:'', //代理服务器地址
                    // proxyType:'', //代理服务器类型
                    // proxyUser:'', //代理服务器用户名
                    // returnAddr:'', //回复地址
                    // serverPort:'',//邮件服务端口号
                    // serverType:'', //服务器类型
                    // timeOut:'',//超时
                    // userName:'' //用户名
                },
                rules:{
                    showName: [
                        {required: true, message: '请输入名称', trigger: 'blur'}
                    ],
                    server: [
                        {required: true, message: '请输入SMTP服务器', trigger: 'blur'}
                    ],
                    sendName: [
                        {required: true, message: '请输入发件人姓名', trigger: 'blur'}
                    ],
                    emailAddr: [
                        {required: true, message: '请输入email邮箱地址', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init(row) {
                this.dialogVisible = true;
                if(JSON.stringify(row)!=='{}'){ //编辑
                    this.form = Object.assign({},row);
                }else{
                    this.form.showName=this.form.server=this.form.sendName=this.form.emailAddr=''
                }
            },
            save() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        if(this.form.id){ //编辑更新
                            update(this.form,1).then(res => {
                                this.dealEnd(res);
                            }).catch(err => {
                                console.log(err)
                            });
                        }else{ //新增
                            add(this.form,1).then(res => {
                                this.dealEnd(res);
                            }).catch(err => {
                                console.log(err)
                            });
                        }
                    } else {
                        return false;
                    }
                });
            },
            dealEnd(res){
                if(res.data.success){
                    this.$refs.ruleForm.resetFields();
                    this.dialogVisible=false;
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                    this.$emit('getData', 1)
                }else{
                    this.$message({
                        type: "success",
                        message: res.data.msg
                    });
                }

            }
        }
    }
</script>

<style scoped>

</style>
